//
//  ResumenView.m
//  snoopifon-bbva
//
//  Created by Jorge Luis Garcia Perez on 23/03/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "ResumenView.h"
#import "ResumenItem.h"
#import "DetallesViewController.h"

#define HEXCOLOR(c) [UIColor colorWithRed:((c>>24)&0xFF)/255.0  green:((c>>16)&0xFF)/255.0  blue:((c>>8)&0xFF)/255.0  alpha:((c)&0xFF)/255.0];

@implementation ResumenView

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	
	UIBarButtonItem *oldBackButton = self.navigationItem.backBarButtonItem;
	UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Volver" style:oldBackButton.style target:oldBackButton.target action:oldBackButton.action];
	self.navigationItem.backBarButtonItem = backButton;
	[backButton release];
	
	self.title = @"Home Banking";
		
	cuentasSectionList = [[NSMutableArray alloc] init];
	[cuentasSectionList addObject: [[ResumenItem alloc] initWithTitulo: @"Caja de Ahorro" valor:@"$1.825,65" extra: @"1825_page.png"]];
	[cuentasSectionList addObject: [[ResumenItem alloc] initWithTitulo: @"Cuenta Corriente" valor:@"$-816,21" extra: @"816_page.png"]];
	
	tarjetasSectionList = [[NSMutableArray alloc] init];
	[tarjetasSectionList addObject: [[ResumenItem alloc] initWithTitulo: @"Visa" valor:@"$1.043,10" extra: @"visa_page.png"]];
	[tarjetasSectionList addObject: [[ResumenItem alloc] initWithTitulo: @"Mastercard" valor:@"$522,70" extra: @"master_page.png"]];
	//[tarjetasSectionList addObject: [[ResumenItem alloc] initWithTitulo: @"Visa 0628" valor:@"$195,00" extra: @"visa_page.png"]];
	//[tarjetasSectionList addObject: [[ResumenItem alloc] initWithTitulo: @"Visa 9573" valor:@"$300,20" extra: @"visa_page.png"]];
	
	prestamosSectionList = [[NSMutableArray alloc] init];	
	[prestamosSectionList addObject: [[ResumenItem alloc] initWithTitulo: @"999-71709" valor:@"$12.420,32" extra: @"prestamos_page.png"]];
	[prestamosSectionList addObject: [[ResumenItem alloc] initWithTitulo: @"999-52356" valor:@"$158.326,20" extra: @"prestamos_page.png"]];
	
	cuentascSectionList = [[NSMutableArray alloc] init];	
	//[cuentascSectionList  addObject: [[ResumenItem alloc] initWithTitulo: @"999-511223/6" valor:@"" extra: @"2196_page.png"]];
	[cuentascSectionList  addObject: [[ResumenItem alloc] initWithTitulo: @"En Dólares" valor:@"u$s17.070,00" extra: @"custodia_page.png"]];
	[cuentascSectionList  addObject: [[ResumenItem alloc] initWithTitulo: @"En Pesos" valor:@"$9.160,20" extra: @"custodia_page.png"]];
	
	self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"banking_back.png"]] ;
	
	
	UIView *containerView =
	[[[UIView alloc]
	  initWithFrame:CGRectMake(0, 0, 320, 44)]
	 autorelease];
	containerView.backgroundColor = [UIColor whiteColor];
	
	UIImageView *logo = [[UIImageView alloc] initWithFrame:CGRectMake(264, 4, 52, 36)];
	logo.image = [UIImage imageNamed: @"loguito.png" ];
	
    UILabel *headerLabel =
	[[[UILabel alloc]
	  initWithFrame:CGRectMake(10, 5, 300, 15)]
	 autorelease];
    headerLabel.text = @"Bienvenido:";
    headerLabel.textColor = HEXCOLOR(0x42434aFF);
    headerLabel.font = [UIFont boldSystemFontOfSize:12];
    headerLabel.backgroundColor = [UIColor clearColor];
	
	UILabel *headerLabel2 =
	[[[UILabel alloc]
	  initWithFrame:CGRectMake(10, 20, 300, 22)]
	 autorelease];
    headerLabel2.text = @"Gonzales, Obdulio";
    headerLabel2.textColor = HEXCOLOR(0x081369FF);
    headerLabel2.font = [UIFont boldSystemFontOfSize:18];
    headerLabel2.backgroundColor = [UIColor clearColor];
	
    [containerView addSubview:headerLabel];
	[containerView addSubview:headerLabel2];
	[containerView addSubview:logo];
	
    self.tableView.tableHeaderView = containerView;	
	self.tableView.backgroundColor =  [UIColor clearColor];
	
	
	
    [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
	NSInteger val = 0;
	
	if(section == 0) {
		val = [cuentasSectionList count];
	}
    if(section == 1) {
		val =  [tarjetasSectionList count];
	}
    if(section == 2) {
		val = [prestamosSectionList count];
	}
    if(section == 3) {
		val = [cuentascSectionList count];
	}
	return val;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	
	NSString *sectionHeader = nil;
	
	if(section == 0) {
		sectionHeader = @"Cuentas";
	}
	if(section == 1) {
		sectionHeader = @"Tarjetas";
	}
	if(section == 2) {
		sectionHeader = @"Préstamos";
	}
	if(section == 3) {
		sectionHeader = @"Cuentas Custodia";
	}
	return sectionHeader;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Value1";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    
	if(indexPath.section == 0){
		cell.textLabel.text = [[cuentasSectionList objectAtIndex:indexPath.row] titulo];
		cell.detailTextLabel.text = [[cuentasSectionList objectAtIndex:indexPath.row] valor];
	} 
	
	if(indexPath.section == 1) {
		cell.textLabel.text = [[tarjetasSectionList objectAtIndex:indexPath.row] titulo];
		cell.detailTextLabel.text = [[tarjetasSectionList objectAtIndex:indexPath.row] valor];
	}
	if(indexPath.section == 2) {
		cell.textLabel.text = [[prestamosSectionList objectAtIndex:indexPath.row] titulo];
		cell.detailTextLabel.text = [[prestamosSectionList objectAtIndex:indexPath.row] valor];
	}
	if(indexPath.section == 3) {
		cell.textLabel.text = [[cuentascSectionList objectAtIndex:indexPath.row] titulo];
		cell.detailTextLabel.text = [[cuentascSectionList objectAtIndex:indexPath.row] valor];
	}
	
	[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
	
    return cell;
}




// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	DetallesViewController *detallesViewController = [[DetallesViewController alloc] init];
    
	if(indexPath.section == 0){
		detallesViewController.imageName = [[cuentasSectionList objectAtIndex:indexPath.row] extra];
		//detallesViewController.title = @"Cuentas";
	} 	
	if(indexPath.section == 1) {
		detallesViewController.imageName = [[tarjetasSectionList objectAtIndex:indexPath.row] extra];
		//detallesViewController.title = [[tarjetasSectionList objectAtIndex:indexPath.row] title];
	}
	if(indexPath.section == 2) {
		detallesViewController.imageName = [[prestamosSectionList  objectAtIndex:indexPath.row] extra];
		//detallesViewController.title = [[prestamosSectionList objectAtIndex:indexPath.row] title];
	}
	if(indexPath.section == 3) {
		detallesViewController.imageName = [[cuentascSectionList objectAtIndex:indexPath.row] extra];
		//detallesViewController.title = @"Cuentas Custodia	";
	}
	
	detallesViewController.windowName = @"Home Banking";
	
	[self.navigationController pushViewController:detallesViewController animated:YES];
	
	[detallesViewController release];
	
}



/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source.
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


- (void)dealloc {
	[cuentasSectionList release];
	[tarjetasSectionList release];
    [super dealloc];
}


@end
